import { useSafeState } from "ahooks";
import React, { PropsWithChildren } from "react";
import theme291jili from "./291jili";
import themeTop646 from "./top646";
export type ThemeContextType = {
  themeName: string;
  themeValue: (typeof themes)["291jili"];
  setTheme?: (themeName: string) => void;
};
type ThemeName = "291jili" | "top646";
const themes = {
  "291jili": theme291jili,
  top646: themeTop646,
};
const existsThemeName = (val: any): val is string => !!(val && themes[val]);

export const ThemeContext = React.createContext<ThemeContextType>({
  themeName: "291jili",
  themeValue: themes["291jili"],
});

const ThemeProvider = ({ children }: PropsWithChildren) => {
  const [themeName, setThemeName] = useSafeState("291jili");
  const setTheme = (value: any) => {
    setThemeName((val) => {
      const newTheme = existsThemeName(value) ? value : val;
      return newTheme;
    });
  };
  const themeValue = themes["291jili"];
  return (
    <ThemeContext.Provider
      value={{
        themeName,
        themeValue,
        setTheme,
      }}
    >
      {children}
    </ThemeContext.Provider>
  );
};
export default ThemeProvider;
